package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.pojo.Postbar;
import com.ruoyi.pojo.PostbarCollect;
import com.ruoyi.service.PostbarCollectService;
import com.ruoyi.service.PostbarService;
import com.ruoyi.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/postbar")
@CrossOrigin
public class PostBarControler {

    @Autowired
    private PostbarService postbarService;
    @Autowired
    private PostbarCollectService postbarCollectService;

    @GetMapping("/list")
    public R<List<Postbar>> list(@RequestParam(required = false) String title,@RequestParam(required = false) String school,@RequestParam(required = false) Integer userid){
        QueryWrapper<Postbar> wrapper = new QueryWrapper<Postbar>();
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(school)) {
            wrapper.eq("school",school);
        }
        if (userid!=null) {
            wrapper.eq("userid",userid);
        }
        List<Postbar> list = postbarService.list(wrapper);
        for (Postbar i:list) {
            String image = i.getImage();
                String[] split = image.split(",");
                i.setImageList(split);
        }
        return  R.success(list);
    }

    @GetMapping("/{id}")
    public R<Postbar> getById(@PathVariable Integer id){
        Postbar postbar = postbarService.getById(id);
        String image = postbar.getImage();
            String[] split = image.split(",");
            postbar.setImageList(split);

        return R.success(postbar);
    }

    @GetMapping("/collectlist")
    public R<List<Postbar>> collectlist(PostbarCollect postbarCollect){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userid",postbarCollect.getUserid());
        List<PostbarCollect> list = postbarCollectService.list(wrapper);
        ArrayList<Postbar> postbarList = new ArrayList<>();
        for (PostbarCollect i: list) {
            Postbar postbar = postbarService.getById(i.getPostbarid());
            String image = postbar.getImage();
                String[] split = image.split(",");
                postbar.setImageList(split);

            postbarList.add(postbar);
        }
        return R.success(postbarList);
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody Postbar postbar){
        postbar.setCreatetime(new Date());
        int i = postbarService.add(postbar);
        if (i > 0) {
            return R.success("添加成功");
        }else{
            return R.error("添加失败");
        }
    }

    @Transactional
    @PostMapping("/remove")
    public R<String> remove(@RequestBody Postbar postbar){
        boolean b = postbarService.removeById(postbar.getId());
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("postbarid",postbar.getId());
        boolean remove = postbarCollectService.remove(wrapper);
        if (b) {
            return R.success("删除成功");
        }else{
            return R.error("删除失败");
        }
    }
}
